﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using ReplayDataFile;
using System.Drawing;
using TestMySqlConnetPoll;
using System.Threading;
using View.Common;
using System.Text.RegularExpressions;
using System.Diagnostics;
using DevExpress.XtraSplashScreen;

namespace View.Replay
{
    public class ReplaySetDlg : Form
    {
        // Fields
        private Button btnAccuratePosition;
        private ComboBox cmbDate;
        private IContainer components = null;
        private System.Windows.Forms.Timer controlSecondPlayTimer;
        private bool isBtnNextSecondPressed = false;
        private bool isBtnPreSecondPressed = false;
        private Label label7;
        private Label label8;
        private int lastReplayRecCount;
        private NumericUpDown numericUpDown1;
        private Label label1;
        private Label label2;
        private TextBox txtTime;
        private Button btnCancel;
        private ReplayProgressBar totalDataBar;
      

        // Events
        //public event ReplayEventHandler ReplayUIEvent;

        // Methods
        public ReplaySetDlg()
        {
            //base.CloseFormWhenPressEnter = false;
            this.InitializeComponent();

            this.totalDataBar.ForeColor = Color.Olive;
            this.totalDataBar.SliderColor = Color.Orange;
        }

        private void btnAccuratePosition_Click(object sender, EventArgs e)
        {
            DateTime accuratePosTimeFromCtrl = this.GetAccuratePosTimeFromCtrl();

            if (accuratePosTimeFromCtrl == DateTime.MinValue)
            {
                MessageBox.Show("时间格式不对，请重新输入");
                return;
            }

            if ((this.totalDataBar.TimeSegmentList.GetMaxTime() < accuratePosTimeFromCtrl) || (this.totalDataBar.TimeSegmentList.GetMinTime().AddMilliseconds((double)-this.totalDataBar.TimeSegmentList.GetMinTime().Millisecond) > accuratePosTimeFromCtrl))
            {
                //MessageBoxShow.Show("设定时间超出历史数据时间范围!", "提示", this);
            }
            else
            {
                List<TimeSpan> list = this.totalDataBar.TimeSegmentList.Time2Positions(accuratePosTimeFromCtrl);
                if ((list != null) && (list.Count > 0))
                {
                    TimeSpan sliderPos = list[0];
                    this.totalDataBar.SetSliderPosition(this, sliderPos);
                    this.totalDataBar.Invalidate();
                }
            }

            MySql.ReadReplayData(accuratePosTimeFromCtrl,30);

            this.Close();
        }

        private void btnNextSecond_MouseDown(object sender, MouseEventArgs e)
        {
            this.controlSecondPlayTimer.Start();
            this.isBtnNextSecondPressed = true;
        }

        private void btnNextSecond_MouseUp(object sender, MouseEventArgs e)
        {
            this.controlSecondPlayTimer.Stop();
            this.isBtnNextSecondPressed = false;
        }

        private void btnPreSecond_Click(object sender, EventArgs e)
        {
            //this.FireReplayEvent(ReplayEventType.PlayPreSecond);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }


        private DateTime GetAccuratePosTimeFromCtrl()
        {
            string str = this.cmbDate.Text.Trim();
            string strTime = this.txtTime.Text.Trim();
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(strTime))
            {
                return DateTime.MinValue;
            }
            string[] strArray = str.Split(new char[] { '年', '月', '日' });
            int year = UtilityFunc.ToInt(strArray[0], 1);
            int month = UtilityFunc.ToInt(strArray[1], 1);
            int day = UtilityFunc.ToInt(strArray[2], 1);
            string[] strArrayTime = strTime.Split(new char[] { ':' });

            if (strArrayTime == null || (strArrayTime != null && strArrayTime.Length < 3))
                return DateTime.MinValue;
            int hour = UtilityFunc.ToInt(strArrayTime[0], 0);
            int minute = UtilityFunc.ToInt(strArrayTime[1], 0);
            int seconde = UtilityFunc.ToInt(strArrayTime[2], 0);
            return new DateTime(year, month, day, hour, minute, seconde);
        }

        public void Init()
        {
            int num;
            //Debug.Assert(((CSMReplayApp)CSMApplication.App).DataSupplier != null);
            this.cmbDate.DropDownStyle = ComboBoxStyle.DropDownList;
            this.totalDataBar.MaxDisTimeLen = new TimeSpan(0x1f, 0, 0, 0);
            this.totalDataBar.SliderType = SliderType.TimeRange;
            this.totalDataBar.SliderColor = Color.Red;
            this.totalDataBar.SliderPositionChanged += new SliderPostionChangedEventHandler(this.totalDataBar_SliderPositionChanged);
            this.totalDataBar.TipTimeFormat = "yyyy-MM-dd HH:mm";
            this.totalDataBar.IsDrawCacheBar = false;
        }

        private void InitCombDayList()
        {
            this.cmbDate.BeginUpdate();
            this.cmbDate.Items.Clear();
            if (this.totalDataBar.TimeSegmentList != null)
            {
                foreach (DateTime time in this.totalDataBar.TimeSegmentList.GetDayList())
                {
                    this.cmbDate.Items.Add(string.Format("{0}年{1}月{2}日", time.Year, time.Month, time.Day));
                }
                this.cmbDate.EndUpdate();
            }
        }

        private void InitializeComponent()
        {
            ReplayDataFile.TimeSegmentList timeSegmentList1 = new ReplayDataFile.TimeSegmentList();
            ReplayDataFile.TimeSegmentList timeSegmentList2 = new ReplayDataFile.TimeSegmentList();
            this.cmbDate = new System.Windows.Forms.ComboBox();
            this.btnAccuratePosition = new System.Windows.Forms.Button();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.txtTime = new System.Windows.Forms.TextBox();
            this.btnCancel = new System.Windows.Forms.Button();
            this.totalDataBar = new View.Replay.ReplayProgressBar();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
            this.SuspendLayout();
            // 
            // cmbDate
            // 
            this.cmbDate.FormattingEnabled = true;
            this.cmbDate.Location = new System.Drawing.Point(47, 8);
            this.cmbDate.Name = "cmbDate";
            this.cmbDate.Size = new System.Drawing.Size(163, 20);
            this.cmbDate.TabIndex = 24;
            // 
            // btnAccuratePosition
            // 
            this.btnAccuratePosition.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnAccuratePosition.Location = new System.Drawing.Point(402, 65);
            this.btnAccuratePosition.Name = "btnAccuratePosition";
            this.btnAccuratePosition.Size = new System.Drawing.Size(75, 23);
            this.btnAccuratePosition.TabIndex = 28;
            this.btnAccuratePosition.Text = "确定";
            this.btnAccuratePosition.UseVisualStyleBackColor = true;
            this.btnAccuratePosition.Click += new System.EventHandler(this.btnAccuratePosition_Click);
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(218, 13);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(29, 12);
            this.label8.TabIndex = 27;
            this.label8.Text = "时间";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(12, 13);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(29, 12);
            this.label7.TabIndex = 27;
            this.label7.Text = "日期";
            // 
            // numericUpDown1
            // 
            this.numericUpDown1.Location = new System.Drawing.Point(400, 7);
            this.numericUpDown1.Name = "numericUpDown1";
            this.numericUpDown1.Size = new System.Drawing.Size(64, 21);
            this.numericUpDown1.TabIndex = 42;
            this.numericUpDown1.Value = new decimal(new int[] {
            10,
            0,
            0,
            0});
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(365, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 12);
            this.label1.TabIndex = 27;
            this.label1.Text = "时长";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(470, 12);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 27;
            this.label2.Text = "(分钟)";
            // 
            // txtTime
            // 
            this.txtTime.Location = new System.Drawing.Point(254, 6);
            this.txtTime.Name = "txtTime";
            this.txtTime.Size = new System.Drawing.Size(100, 21);
            this.txtTime.TabIndex = 43;
            this.txtTime.TextChanged += new System.EventHandler(this.TxtTime_TextChanged);
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(483, 65);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 28;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // totalDataBar
            // 
            this.totalDataBar.BarBorderColor = System.Drawing.Color.Black;
            this.totalDataBar.BottomBlank = 1;
            this.totalDataBar.CachedDataColor = System.Drawing.Color.DarkGray;
            this.totalDataBar.CachedTimeSegmentList = timeSegmentList1;
            this.totalDataBar.ClickSkipTimeLen = System.TimeSpan.Parse("00:30:00");
            this.totalDataBar.ForeColor = System.Drawing.Color.LightGray;
            this.totalDataBar.IsDrawCacheBar = false;
            this.totalDataBar.LeftBlank = 0;
            this.totalDataBar.Location = new System.Drawing.Point(0, 39);
            this.totalDataBar.MaxDisTimeLen = System.TimeSpan.Parse("05:00:00");
            this.totalDataBar.Name = "totalDataBar";
            this.totalDataBar.RightBlank = 1;
            this.totalDataBar.Size = new System.Drawing.Size(565, 16);
            this.totalDataBar.SliderColor = System.Drawing.Color.DarkGray;
            this.totalDataBar.SliderSize = new System.Drawing.Size(10, 15);
            this.totalDataBar.SliderTimeRangeLen = System.TimeSpan.Parse("00:00:00");
            this.totalDataBar.SliderType = View.Replay.SliderType.Block;
            this.totalDataBar.TabIndex = 41;
            this.totalDataBar.TextColor = System.Drawing.Color.Blue;
            this.totalDataBar.TimeSegmentList = timeSegmentList2;
            this.totalDataBar.TipTimeColor = System.Drawing.Color.Black;
            this.totalDataBar.TipTimeFormat = "HH:mm:ss";
            this.totalDataBar.TopBlank = 0;
            // 
            // ReplaySetDlg
            // 
            this.ClientSize = new System.Drawing.Size(566, 94);
            this.ControlBox = false;
            this.Controls.Add(this.txtTime);
            this.Controls.Add(this.numericUpDown1);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnAccuratePosition);
            this.Controls.Add(this.totalDataBar);
            this.Controls.Add(this.cmbDate);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.label7);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Location = new System.Drawing.Point(50, 600);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ReplaySetDlg";
            this.Text = "回放数据选择";
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }




        public void SetPositionInTotalTime(object setter, TimeSpan pos)
        {
            this.totalDataBar.SetSliderPosition(setter, pos);
        }



        private void totalDataBar_SliderPositionChanged(object setter, TimeSegment timeSeg, DateTime time, bool isDraging)
        {
            this.UpdateTimeSelCtrl(time);
        }


        private void UpdateTimeSelCtrl(DateTime time)
        {
            this.cmbDate.Text = string.Format("{0}年{1}月{2}日", time.Year, time.Month, time.Day);
            this.txtTime.Text = string.Format("{00}:{01}:{02}", time.Hour, time.Minute, time.Second);
        }


        public TimeSegmentList TotalTimeList
        {
            get
            {
                return this.totalDataBar.TimeSegmentList;
            }
            set
            {
                this.totalDataBar.TimeSegmentList = value;
                if (value != null)
                {
                    this.totalDataBar.MaxDisTimeLen = value.GetTotalTimeLen();
                }
                else
                {
                    this.totalDataBar.MaxDisTimeLen = new TimeSpan();
                }
                this.InitCombDayList();
            }
        }

        private void TxtTime_TextChanged(object sender, EventArgs e)
        {
            //if (!Regex.IsMatch(this.txtTime.Text, "^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$"))
            // {
            //     this.txtTime.Text = "00:00:00";
            // }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
